package com.android.lovingpet.mine.myInformation;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;


import com.android.lovingpet.base.BasePresenter;
import com.android.lovingpet.common.Back;
import com.android.lovingpet.common.FileModel;
import com.android.lovingpet.common.Model;
import com.android.lovingpet.bean.Account;
import com.android.lovingpet.common.UserModel;
import com.android.lovingpet.util.Utility;

import java.io.File;
import java.util.HashMap;

/**
 * Created by ikata on 2018/3/4.
 */

public class MyinformationPresenter extends BasePresenter<IMyinformationContract.View> implements IMyinformationContract.Presenter{

    @Override
    public void changeInfo(File image, int id, int userType, String name, int gender, String synopsis, String city) {
        if (TextUtils.isEmpty(name)) {
            getView().showToast("用户名不能为空");
            return;
        }

        getView().showProgress();
        new MyinformationModel().changeInfo(name, gender, synopsis, city, new Back() {
            @Override
            public void onFailure(String msg) {
                getView().hideProgress();
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                if (image == null) {
                    getInfo(id);
                } else {
                    changeImage(image, id, userType);
                }
            }
        });
    }

    private void changeImage(File image, int id, int userType) {
        FileModel.getInstance().uploadImage(id, userType, image, new Back() {
            @Override
            public void onFailure(String msg) {
                getView().hideProgress();
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                getInfo(id);
            }
        });
    }

    private void getInfo(int id) {
        UserModel.getInstance().getInfo(id, new FileModel.FileBack() {
            Account account;
            @Override
            public void loadSuccess(String url) {
                account.setHead(url);
                Utility.setAccount(account);
                getView().next();
            }

            @Override
            public void onFailure(String msg) {
                getView().hideProgress();
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                account = bundle.getParcelable("account");
            }
        });
    }
}
